package sk.fei.vrml.jogl;

import java.util.LinkedList;

import javax.swing.tree.DefaultMutableTreeNode;

import sk.fei.vrml.gui.MainFrame;
import sk.fei.vrml.objects.VrmlBackground;
import sk.fei.vrml.objects.VrmlObject;
import sk.fei.vrml.vrmlio.VrmlIO;

/**
 * @author anubis
 *
 */
/**
 * @author anubis
 * 
 */
public class VrmlModel {
	private LinkedList<VrmlObject> modelComponents;
	private MainFrame window;
	private VrmlIO ioobject;
	private boolean modelChanged;
	private VrmlBackground background;

	public VrmlModel(MainFrame w) {
		modelComponents = new LinkedList<VrmlObject>();
		window = w;
		ioobject = new VrmlIO(modelComponents, window);
		modelChanged = false;
		background = new VrmlBackground(null,null);
	}

	public VrmlIO getIoobject() {
		return ioobject;
	}

	public LinkedList<VrmlObject> getModelComponents() {
		return modelComponents;
	}
	
	public void removeVrmlObject(VrmlObject obj){
		modelChanged = true;
		synchronized(this) {
			modelComponents.remove(obj);
		}
	}

	public DefaultMutableTreeNode addComponentToModel(VrmlObject o) {
		modelChanged = true;
		synchronized(this) {
			modelComponents.add(o);
			return window.addNodeToTree(o);
		}
	}
	//TODO toto som vytvoril podla metody hore ale tiez to asi neni treba pre strom
	public void addViewPointToModel(VrmlObject o) {
		modelChanged = true;
		synchronized(this) {
			modelComponents.add(o);
			window.addViewPointNodeToTree(o);
		}
	}
	
	public DefaultMutableTreeNode addTransformationToModel(VrmlObject o,DefaultMutableTreeNode root) {
		modelChanged = true;
		synchronized(this) {
			modelComponents.add(o);
			return window.addChildTransformationToNode(o,root );
		}
	}
	
	public boolean wasModelChanged(){
		return modelChanged;
	}
	
	public void resetModelChanged(){
		modelChanged = false;
	}
	
	public void setModelChanged(){
		modelChanged = true;
	}
	
	public void setModelBackground(VrmlBackground back){
		background = back;
		modelComponents.add(background);
	}
	
	public VrmlBackground getModelBackground(){
		return background;
	}
	
	public boolean isBackground(){
		return !background.isEmpty();
	}
	
	public void addBackgroundAsComponent(){
		if (!isBackground()) modelComponents.add(background);
	}


}
